package com.subsequent.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.subsequent.pojo.Doctors;
import com.subsequent.service.DoctorsService;
import com.subsequent.utils.QiniuUtils;
import com.subsequent.utils.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/doctor")
public class DoctorController {
    @Resource
    private DoctorsService doctorsService;

    @GetMapping("/findDoctorById/{userId}")
    public Result findDoctorById(@PathVariable("userId") Integer userId) {
        return doctorsService.findDoctorById(userId);
    }
    @PostMapping("/updateIntroduction")
    public Result updateIntroduction(@RequestBody Doctors doctors){
        return doctorsService.updateIntroduction(doctors);
    }
    @PostMapping("/updateSpecialty")
    public Result updateSpecialty(@RequestBody Doctors doctors){
        return doctorsService.updateSpecialty(doctors);
    }
    @RequestMapping("/uploadPhoto")
    public Result uploadPhoto(@RequestParam("file") MultipartFile file,@RequestParam("userId") Integer userId) throws IOException {

        try {
            String filename = file.getOriginalFilename();
            int index = filename.lastIndexOf(".");
            String substring = filename.substring(index);
            String s = UUID.randomUUID().toString() + substring;
            QiniuUtils.upload2Qiniu(file.getBytes(),s);
            QueryWrapper<Doctors>queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(Doctors::getUserId,userId);
            Doctors one = doctorsService.getOne(queryWrapper);
            one.setPhoto(s);
            doctorsService.updateById(one);
            return Result.success(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("服务异常");
    }
}
